package com.convert.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.convert.Constants.ConvertResult;

public class ExtractUtil {
    
    public static List<String> getSources(String payload){
        List<String> result = new ArrayList<String>();
        String[] strings = payload.split("\\{\"source\"");
        if(!payload.startsWith("{\"source")) {
            strings = Arrays.copyOfRange(strings, 1, strings.length);
        }
        for(String s: strings) {
            if(StringUtils.isNotBlank(s)) {
                s = "{\"source\"" + s;
                result.add(s);
            }
        }
        
        return result;
    }
    
    public static List<String> extractSource(List<String> sources) {
        
       return sources.stream()
               .filter(s -> (s.contains(ConvertResult.REQUEST_BODY) || s.contains(ConvertResult.RESPONSE_BODY)))
               .collect(Collectors.toList());
        
        
    }
}
